import 'package:syria_sports/presentation/admin_home_tabs/coaches_tab/widgets/coach_item_view.dart';

import '../../../../core/app_export.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/custom_rating_bar.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../add_coach_page/bloc/add_coach_page_screen.dart';
import 'coaches_tab_cubit.dart';
import 'package:flutter/material.dart';

class AllCoachesScreen extends StatelessWidget {
  const AllCoachesScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage(ImageConstant.imgBackground),
          fit: BoxFit.cover,
        ),
      ),
      child: Scaffold(
        appBar: _buildAppBar(context: context),
        body: BlocProvider<CoachesTabCubit>(
          create: (context) => CoachesTabCubit()..getAllUsers(),
          child: BlocConsumer<CoachesTabCubit, CoachesTabState>(
            listener: (context, state) {
              // TODO: implement listener
            },
            builder: (context, state) {
              var cubit = CoachesTabCubit.get(context);

              return Container(
                height: height,
                width: width,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage(ImageConstant.imgBackground),
                    fit: BoxFit.cover,
                  ),
                ),
                child: ScreenStateWidget(
                  isDone: state is CoachesTabDone,
                  isError: state is CoachesTabError,
                  isLoading: state is CoachesTabLoading,
                  onRefresh: cubit.getAllUsers,
                  onDone: GridView.builder(
                      gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
                        maxCrossAxisExtent: 400,
                        mainAxisExtent: 150,
                        //childAspectRatio:(height>width)? height/width:width/height,
                      ),
                      itemCount: cubit.users.length,
                      itemBuilder: (context,index){
                        return CoachItemView(user: cubit.users[index]);
                      }),
                ),

              );
            },
          ),
        )
      ),
    );
  }
  PreferredSizeWidget _buildAppBar({required BuildContext context}) {
    return CustomAppBar(
      actions: [
        if (PrefUtils.isAdmin())
          OutlineIconButton(
          icon: Icons.add,
          margin:const EdgeInsets.all(8),
          padding:const EdgeInsets.all(2),
          onTapped: ()async{
            await Navigator.push(context, MaterialPageRoute(builder: (context) => const AddCoachPageScreen())).then((value) {
              CoachesTabCubit.get(context).getAllUsers();
            });
          },
        ),
      ],
      leading: Row(
        children: [
          OutlineIconButton(
            imagePath: ImageConstant.imgRightIcon,
            onTapped: (){
              Navigator.pop(context);
            },
          ),
        ],
      ),
      title: AppbarSubtitle(text: 'المدربين'),
    );
  }
}
